package com.changgou.service.goods.controller;


import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;

    @GetMapping("/findAll")
    public Result<List<Brand>> findList() {
        List<Brand> list = brandService.list();
        return new Result<>(true, StatusCode.OK, "success~", list);
    }

    @GetMapping("/findById/{id}")
    public Result<Brand> findById(@PathVariable("id") Integer id) {
        return new Result<>(true, StatusCode.OK, "success", brandService.findById(id));
    }

    @PostMapping("/addBrand")
    public Result addBrand(@RequestBody Brand brand) {
        brandService.addBrand(brand);
        return new Result(true, StatusCode.OK, "success");
    }

    @PutMapping("/updateBrand")
    public Result updateBrand(@RequestBody Brand brand) {
        brandService.updateBrand(brand);
        return new Result(true, StatusCode.OK, "修改成功");

    }
    @DeleteMapping("/deleteBrandById/{id}")
    public Result deleteBrandById(@PathVariable("id") Integer id){
        brandService.deleteBrandById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestParam Map searchMap){
        List list = brandService.list(searchMap);
        return new Result<>(true, StatusCode.OK, "success~", list);
    }

    @GetMapping("/findPage/{page}/{size}")
    public PageResult<Brand> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Page<Brand> page1 = brandService.findPage(page, size);

        return new PageResult(page1.getTotal(),page1.getResult());
    }

    @GetMapping("/findPageCriteria/{page}/{size}")
    public PageResult<Brand> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size,@RequestParam Map<String,Object> searchMap){
        Page<Brand> page1 = brandService.findPage(page, size,searchMap);
        return new PageResult(page1.getTotal(),page1.getResult());
    }
}
